/**
  ******************************************************************************
  * @file    mass_mal.c
  * @author  MCD Application Team
  * @version V3.4.0
  * @date    29-June-2012
  * @brief   Medium Access Layer interface
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
  *
  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  * You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
  *
  *        http://www.st.com/software_license_agreement_liberty_v2
  *
  * Unless required by applicable law or agreed to in writing, software 
  * distributed under the License is distributed on an "AS IS" BASIS, 
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */


/* Includes ------------------------------------------------------------------*/
#include "platform_config.h"
#include "mass_mal.h"
//#include "sdio_sd.h"
//#include "stm32_eval_sdio_sd.h"
#include "drivers.h"
#include <stdio.h>

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
uint32_t Mass_Memory_Size[2];
uint32_t Mass_Block_Size[2];
uint32_t Mass_Block_Count[2];
__IO uint32_t Status = 0;

//#if defined(USE_STM3210E_EVAL) || defined(USE_STM32L152D_EVAL)
//SD_CardInfo mSDCardInfo;
//#endif

/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/*******************************************************************************
* Function Name  : MAL_Init
* Description    : Initializes the Media on the STM32
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
uint16_t MAL_Init(uint8_t lun)
{
	//SPI_FLASH_Init();
	//printf("MAL sFlash Init success\n");
	return MAL_OK;           //Init success
}
/*******************************************************************************
* Function Name  : MAL_Write
* Description    : Write sectors
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
uint16_t MAL_Write(uint8_t lun, uint32_t Memory_Offset, uint8_t *Writebuff, uint16_t Transfer_Length)
{
	//printf("Write %d %d %d\n",Memory_Offset,Writebuff,Transfer_Length);
	//printf("w lun :%d\n",lun);
  switch (lun)
  {
    //case _SD_CARD:
		case 0:
				SPI_FLASH_SectorErase(Memory_Offset);
				SPI_FLASH_BufferWrite((u8*) Writebuff, Memory_Offset, 4096);
			break;
		default:
      return MAL_FAIL;
	} 
  return MAL_OK;

}

/*******************************************************************************
* Function Name  : MAL_Read
* Description    : Read sectors
* Input          : None
* Output         : None
* Return         : Buffer pointer
*******************************************************************************/
uint16_t MAL_Read(uint8_t lun, uint32_t Memory_Offset, uint8_t *Readbuff, uint16_t Transfer_Length)
{
	//printf("Read %d %d %d\n",Memory_Offset,Readbuff,Transfer_Length);
	//printf("r lun :%d\n",lun);
  switch (lun)
  {
    case 0:
			//printf("DRd len = %d\n",Transfer_Length);
			SPI_FLASH_BufferRead((u8*) Readbuff, Memory_Offset, 4096);
			break;
    default:
      return MAL_FAIL;
  }
	return MAL_OK;
}

/*******************************************************************************
* Function Name  : MAL_GetStatus
* Description    : Get status
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
uint16_t MAL_GetStatus (uint8_t lun)
{
	//unsigned long a,b;
//	uint32_t DeviceSizeMul = 0, NumberOfBlocks = 0;
	if (lun == 0)
	{
    Mass_Block_Size[0]  = 4096;
		//Mass_Block_Count[0] =	512;
		Mass_Block_Count[0] =	2048;
		Mass_Memory_Size[0] = (Mass_Block_Count[0] * Mass_Block_Size[0]);
		/*a = SDCardInfo.SD_csd.DeviceSize + 1;
		b = 1 << (SDCardInfo.SD_csd.DeviceSizeMul + 2);  
		
		Mass_Block_Count[0] = a * b;
		Mass_Block_Size[0]	= 1<<SDCardInfo.SD_csd.RdBlockLen;
		Mass_Memory_Size[0] = (Mass_Block_Count[0] * Mass_Block_Size[0]);
		
		//printf("MAL_GetStatus %d %d %d\n",Mass_Block_Count[0],Mass_Block_Size[0],Mass_Memory_Size[0]);
		*/
		return MAL_OK;
	}
	return MAL_FAIL;
}

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

